package com.tf_cut.backend.seller.service;


import com.alibaba.fastjson.JSON;
import com.tf_cut.backend.common.pojo.PageInfo;
import com.tf_cut.backend.common.pojo.Result;
import com.tf_cut.backend.common.service.CommonServiceImpl;
import com.tf_cut.backend.mapper.SellerAndAccountMapper;
import com.tf_cut.backend.seller.pojo.Seller;
import com.tf_cut.backend.seller.repository.SellerRepository;
import com.tf_cut.backend.seller.vo.SellerAndAccountVo;
import com.tf_cut.backend.seller.vo.SellerAndServiceVo;
import com.tf_cut.backend.util.UUIDUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.List;

@Service
@Transactional
@Slf4j
public class SellerServiceImpl extends CommonServiceImpl<SellerAndAccountVo, Seller, String> implements SellerService {

    @PersistenceContext
    private EntityManager em;
    @Autowired
    private SellerRepository sellerRepository;
    @Autowired
    private SellerAccountService sellerAccountService;
	@Autowired
	private SellerAndAccountMapper sellerAndAccountMapper;
    
	@Override
	public Result<SellerAndAccountVo> insertSellerAndAccount(SellerAndAccountVo svo) {
		System.out.println(svo.getIsOld()+"==================================");
		Result<SellerAndAccountVo> result = this.save(svo);	// 新建商户
		svo.setSellerId(result.getData().getSellerId());
		
		svo.setSellerAccount(UUIDUtil.getUUID());
		result = sellerAccountService.save(svo);
		svo.setSellerAccountId(result.getData().getSellerAccountId());	// 新建商户虚拟账号
		
		result.setData(svo);
		
		return result;
	}

	@Override
	public Result<Boolean> insertSellers(String json) {
		Result<Boolean> result = Result.of(true);
		List<SellerAndAccountVo> importData = JSON.parseArray(json, SellerAndAccountVo.class);
		int num = 0;
		try {
			for (SellerAndAccountVo seller : importData) {
				switch (num++) {
				}
	            if (StringUtils.isEmpty(seller.getSellerName())) {
	            	result = Result.of(false);
	            	result.setMsg("导入失败！第" + num + "行的商户名称不能为空");
	            	result.setFlag(false);
	                return result;
	            }
	            seller.setSellerState("N");//未冻结
	            this.insertSellerAndAccount(seller);
	        }
		} catch(Exception e) {
			log.error("创建第" + num + "行商户失败。error="+e.getMessage(),e);
			result = Result.of(false);
			result.setMsg("创建第" + num + "行商户失败");
			result.setFlag(false);
		}
        return result;
	}

	@Override
	public PageInfo<SellerAndAccountVo> querySellerBillList(SellerAndAccountVo sellerAndAccountVo) {
		com.github.pagehelper.PageHelper.startPage(sellerAndAccountVo.getPage(), sellerAndAccountVo.getRows());
		List<SellerAndAccountVo> list = sellerAndAccountMapper.findSellerForAdmin(sellerAndAccountVo);
		com.github.pagehelper.PageInfo<SellerAndAccountVo> pageInfo = new com.github.pagehelper.PageInfo<>(list);

		PageInfo<SellerAndAccountVo> returnPageInfo = new PageInfo<>();
		returnPageInfo.setPage(pageInfo.getPageNum());
		returnPageInfo.setPageSize(pageInfo.getPageSize());
		returnPageInfo.setRows(pageInfo.getList());
		returnPageInfo.setRecords((int) pageInfo.getTotal());
		returnPageInfo.setTotal(pageInfo.getPages());
		return returnPageInfo;
	}

	@Override
	public PageInfo<SellerAndAccountVo> querySellerBalance(SellerAndAccountVo sellerAndAccountVo) {
		com.github.pagehelper.PageHelper.startPage(sellerAndAccountVo.getPage(), sellerAndAccountVo.getRows());
		List<SellerAndAccountVo> list = sellerAndAccountMapper.querySellerBalance(sellerAndAccountVo);
		com.github.pagehelper.PageInfo<SellerAndAccountVo> pageInfo = new com.github.pagehelper.PageInfo<>(list);

		PageInfo<SellerAndAccountVo> returnPageInfo = new PageInfo<>();
		returnPageInfo.setPage(pageInfo.getPageNum());
		returnPageInfo.setPageSize(pageInfo.getPageSize());
		returnPageInfo.setRows(pageInfo.getList());
		returnPageInfo.setRecords((int) pageInfo.getTotal());
		returnPageInfo.setTotal(pageInfo.getPages());
		return returnPageInfo;
	}

	@Override
	public List<SellerAndServiceVo> findSellerService(SellerAndServiceVo avo) {
		
		return sellerAndAccountMapper.findSellerService(avo);
	}
}
